using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System.Data.Common;

namespace Colorprj.Masters
{
    public class SalesManManager
    {

        public DataSet GetAllSalesMan()
        {
            // Create the Database object, using the default database service. The
            // default database service is determined through configuration.
            Database db = DatabaseFactory.CreateDatabase();

            string sqlCommand = "SelAllSalesMan";
            DbCommand selSPCommand = db.GetStoredProcCommand(sqlCommand);
            // DataSet that will hold the returned results		
            DataSet productsDataSet = null;

            productsDataSet = db.ExecuteDataSet(selSPCommand);

            // Note: connection was closed by ExecuteDataSet method call 

            return productsDataSet;
        }

        public bool AddSalesMan(SalesMan  sEntity)
        {
            bool result = false;
            Database db = DatabaseFactory.CreateDatabase();
            using (DbConnection connection = db.CreateConnection())
            {
                connection.Open();
                DbTransaction transaction = connection.BeginTransaction();

                try
                {


                    // Two operations, one to credit an account, and one to debit another
                    // account.
                    string sqlCommand = "AddSalesMan";
                    DbCommand addGroupCommand = db.GetStoredProcCommand(sqlCommand);
                    db.AddInParameter(addGroupCommand, "SalesManName", DbType.String, sEntity.SalesManName);
                    db.AddInParameter(addGroupCommand, "SalesManAddress1", DbType.String, sEntity.SalesManAddress1);
                    db.AddInParameter(addGroupCommand, "SalesManAddress2", DbType.String, sEntity.SalesManAddress2);
                    db.AddInParameter(addGroupCommand, "SalesManCity", DbType.String, sEntity.SalesManCity);
                    db.AddInParameter(addGroupCommand, "PhoneNo", DbType.String, sEntity.PhoneNo);
                    db.AddInParameter(addGroupCommand, "EmailId", DbType.String, sEntity.EmailId);


                    // Credit the first account
                    db.ExecuteNonQuery(addGroupCommand, transaction);

                    transaction.Commit();
                    result = true;
                }
                catch (Exception ex)
                {
                    transaction.Rollback();
                    throw new Exception(ex.Message);
                }
            }
            return result;
        }

        public bool UpdSalesMan(SalesMan sEntity)
        {
            bool result = false;
            Database db = DatabaseFactory.CreateDatabase();
            using (DbConnection connection = db.CreateConnection())
            {
                connection.Open();
                DbTransaction transaction = connection.BeginTransaction();

                try
                {


                    // Two operations, one to credit an account, and one to debit another
                    // account.
                    string sqlCommand = "UpdSalesMan";
                    DbCommand addGroupCommand = db.GetStoredProcCommand(sqlCommand);
                    db.AddInParameter(addGroupCommand, "SalesManId", DbType.String, sEntity.SalesManId);
                    db.AddInParameter(addGroupCommand, "SalesManName", DbType.String, sEntity.SalesManName);
                    db.AddInParameter(addGroupCommand, "SalesManAddress1", DbType.String, sEntity.SalesManAddress1);
                    db.AddInParameter(addGroupCommand, "SalesManAddress2", DbType.String, sEntity.SalesManAddress2);
                    db.AddInParameter(addGroupCommand, "SalesManCity", DbType.String, sEntity.SalesManCity);
                    db.AddInParameter(addGroupCommand, "PhoneNo", DbType.String, sEntity.PhoneNo);
                    db.AddInParameter(addGroupCommand, "EmailId", DbType.String, sEntity.EmailId);


                    // Credit the first account
                    db.ExecuteNonQuery(addGroupCommand, transaction);

                    transaction.Commit();
                    result = true;
                }
                catch (Exception ex)
                {
                    transaction.Rollback();
                    throw new Exception(ex.Message);
                }
            }
            return result;
        }

        public void DelSalesMan(int Id)
        {

            // Create the Database object, using the default database service. The
            // default database service is determined through configuration.
            try
            {
                Database db = DatabaseFactory.CreateDatabase();
                string sqlCommand = "DelSalesMan";
                DbCommand selSPCommand = db.GetStoredProcCommand(sqlCommand);
                db.AddInParameter(selSPCommand, "Id", DbType.Int32, Id);
                db.ExecuteNonQuery(selSPCommand);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }

        }

    }
}
